package com.central.apps.handler;

import com.central.app.service.HandlerService;
import com.central.apps.common.util.DateUtiles;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.util.ToolUtil;
import lombok.Data;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class TransferapplyheadHandler extends LvBaseHandler {

    @Autowired
    private HandlerService handlerService;

    /*@Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        String message =null;
            if(type.equals("transfer")){
            message ="子表无数据";
            String transfernum = MapUtils.getString(params,"transfernum");
            List<Map<String, Object>> bySql = handlerService.findBySql("select id from gq_transferapplydetail where transfernum ='" + transfernum + "'");
            if(!ToolUtil.isEmpty(bySql)){
                String id =MapUtils.getString(bySql.get(0),"id");
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select assetnum from gq_assethead order by createtime desc  limit 1");
                String assetnum =MapUtils.getString(bySql1.get(0),"assetnum");
                String childNum =getNum(assetnum);
                handlerService.updateBySql("update gq_transferapplydetail set assetnum ='"+childNum+"' where id ="+id);
                for(int i=1;i<bySql.size();i++){
                    Map<String, Object> map =bySql.get(i);
                    String id1 =MapUtils.getString(map,"id");
                    List<Map<String, Object>> bySql2 = handlerService.findBySql("select assetnum from gq_transferapplydetail order by assetnum desc limit 1");
                    String assetnum1 =MapUtils.getString(bySql2.get(0),"assetnum");
                    String childNum1 =getNum(assetnum1);
                    handlerService.updateBySql("update gq_transferapplydetail set assetnum ='"+childNum1+"' where id ="+id1);
                }
                message ="子表资产编号已生成";
            }
        }
        return message;
    }*/

    public String getNum(String assetnum){
        int num;
        if(assetnum.indexOf("0")!=-1){
            if(assetnum.endsWith("0")){
                String assetnum1=assetnum.substring(2);
                num =Integer.parseInt(assetnum1);
                num++;
            }else {
                int index = assetnum.lastIndexOf("0");
                String assetnum1 = assetnum.substring(index + 1);
                num = Integer.parseInt(assetnum1);
                num++;
            }
        }else{
            String assetnum1=assetnum.substring(2);
            num =Integer.parseInt(assetnum1);
            num++;
        }
        String format = String.format("%05d", num);
        String result ="as"+format;
        return result;
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        String transfernum = MapUtils.getString(data,"transfernum");
        List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferapplydetail where transfernum ='"+transfernum+"'");
        if(ToolUtil.isNotEmpty(bySql1)){
            for(Map<String, Object> map :bySql1){
                // 1.获取参数数据
                String id =MapUtils.getString(map,"id");
                int useryear = (int) map.get("useryear");
                java.sql.Timestamp productiontime = (java.sql.Timestamp) map.get("productiontime");

                // 2.计算资产剩余寿命(月)：使用寿命-（当前时间-投产时间）
                Date currentDate = new Date();
                int intervalMonth = DateUtiles.calIntervalMonth(productiontime, currentDate);

                if (intervalMonth > useryear) {
                    intervalMonth = 0;
                } else {
                    intervalMonth = useryear - intervalMonth;
                }

                // 3.更新资产台账资产剩余寿命(月)&sql信息
                String updSql = "update gq_transferapplydetail set surpluslife ='" + intervalMonth + "' where id =" + id;
                handlerService.updateBySql(updSql);
            }

        }
    }

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        if (flowNode.getId().equals("fqr")) {
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_transferapplyhead where id ="+id);
            String transfernum =MapUtils.getString(bySql.get(0),"transfernum");
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferapplydetail where transfernum ='"+transfernum+"'");
            if(ToolUtil.isEmpty(bySql1)){
                return "子表数据为空";
            }
        }
        return null;
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if (flowNode.getId().equals("zg")) {
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_transferapplyhead where id ="+id);
            String transfernum =MapUtils.getString(bySql.get(0),"transfernum");
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferapplydetail where transfernum ='"+transfernum+"'");
            if(!ToolUtil.isEmpty(bySql1)){
                String sonid =MapUtils.getString(bySql1.get(0),"id");
                List<Map<String, Object>> bySql2 = handlerService.findBySql("select assetnum from gq_assethead order by createtime desc  limit 1");
                String assetnum =MapUtils.getString(bySql2.get(0),"assetnum");
                String childNum =getNum(assetnum);
                handlerService.updateBySql("update gq_transferapplydetail set assetnum ='"+childNum+"' where id ="+sonid);
                for(int i=1;i<bySql1.size();i++){
                    Map<String, Object> map =bySql1.get(i);
                    String id1 =MapUtils.getString(map,"id");
                    List<Map<String, Object>> bySql3 = handlerService.findBySql("select assetnum from gq_transferapplydetail order by assetnum desc limit 1");
                    String assetnum1 =MapUtils.getString(bySql3.get(0),"assetnum");
                    String childNum1 =getNum(assetnum1);
                    handlerService.updateBySql("update gq_transferapplydetail set assetnum ='"+childNum1+"' where id ="+id1);
                }
            }
            List<Map<String, Object>> bySql7 = handlerService.findBySql("select * from gq_transferapplydetail where transfernum ='"+transfernum+"'");
            for(Map<String, Object> map : bySql7){
                String assetnum = MapUtils.getString(map,"assetnum");
                String description = MapUtils.getString(map,"description");
                String status = MapUtils.getString(map,"status");
                String createuserdesc = MapUtils.getString(map,"createuserdesc");
                String location = MapUtils.getString(map,"location");
                String locationdesc = MapUtils.getString(map,"locationdesc");
                String installsite = MapUtils.getString(map,"installsite");
                String classify = MapUtils.getString(map,"classify");
                String classifydescribe = MapUtils.getString(map,"classifydescribe");
                String assettype = MapUtils.getString(map,"assettype");
                String updateuser = MapUtils.getString(map,"updateuser");
                String parent = MapUtils.getString(map,"parent");
                String parentdesc = MapUtils.getString(map,"parentdesc");
                String assetuser = MapUtils.getString(map,"assetuser");
                String updatetime = MapUtils.getString(map,"updatetime");
                String isspecequipment = MapUtils.getString(map,"isspecequipment");
                String assetadmin = MapUtils.getString(map,"assetadmin");
                String deptdesc = MapUtils.getString(map,"deptdesc");
                String usedepartment = MapUtils.getString(map,"usedepartment");
                String responsible = MapUtils.getString(map,"responsible");
                String productcode = MapUtils.getString(map,"productcode");
                String specmodel = MapUtils.getString(map,"specmodel");
                String leavefactorycode = MapUtils.getString(map,"leavefactorycode");
                String originalvalue = MapUtils.getString(map,"originalvalue");
                String purchaseprice = MapUtils.getString(map,"purchaseprice");
                String deliverytime = MapUtils.getString(map,"deliverytime");
                String manufacturer = MapUtils.getString(map,"manufacturer");
                String supplier = MapUtils.getString(map,"supplier");
                String installtime = MapUtils.getString(map,"installtime");
                String manufactorperson = MapUtils.getString(map,"manufactorperson");
                String manufactorphone = MapUtils.getString(map,"manufactorphone");
                String productiontime = MapUtils.getString(map,"productiontime");
                String useryear = MapUtils.getString(map,"useryear");
                String surpluslife = MapUtils.getString(map,"surpluslife");
                Date now = new Date();
                SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String createtime = sdf.format(now);
                String sql ="insert into gq_assethead (assetnum,description,status,createuserdesc,location,locationdesc,"
                        +"installsite,createtime,classify,classifydescribe,assettype,updateuser,parent,parentdesc,"
                        +"assetuser,updatetime,isspecequipment,assetadmin,deptdesc,usedepartment,responsible,"
                        +"productcode,specmodel,leavefactorycode,originalvalue,purchaseprice,deliverytime,"
                        +"manufacturer,supplier,installtime,manufactorperson,manufactorphone,productiontime,"
                        +"useryear,surpluslife"
                        +") values('"
                        +assetnum+"','"+description+"','"+status+"','"+createuserdesc+"','"+location+"','"+locationdesc+"','"+installsite+"','"
                        +createtime+"','"+classify+"','"+classifydescribe+"','"+assettype+"','"+updateuser+"','"+parent+"','"+parentdesc+"','"
                        +assetuser+"','"+updatetime+"','"+isspecequipment+"','"+assetadmin+"','"+deptdesc+"','"+usedepartment+"','"+responsible+"','"
                        +productcode+"','"+specmodel+"','"+leavefactorycode+"','"+originalvalue+"','"+purchaseprice+"','"+deliverytime+"','"
                        +manufacturer+"','"+supplier+"','"+installtime+"','"+manufactorperson+"','"+manufactorphone+"','"+productiontime+"','"
                        +useryear+"','"+surpluslife+"'"
                +")";
                handlerService.updateBySql(sql);

            }
        }
    }
}
